//
//  SpeechManager.m
//  AudioConvertOC
//
//  Created by xyanl on 2025/4/26.
//

#import "SpeechManager.h"
#import <Speech/Speech.h>

@interface SpeechManager ()<SFSpeechRecognitionTaskDelegate>

@property (nonatomic ,strong) SFSpeechRecognitionTask *recognitionTask;

@property(nonatomic,strong) SFSpeechRecognizer *recognizer;


@property (nonatomic, copy) CompleteBlock completeBlock;

@end

@implementation SpeechManager

+ (instancetype)shareInstance
{
    static SpeechManager * singleClass = nil;
    static dispatch_once_t onceToken ;
    dispatch_once(&onceToken, ^{
        singleClass = [[SpeechManager alloc] init] ;
    }) ;
    
    return singleClass ;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self requestAuthorization];
    }
    return self;
}


- (void)createSpeech:(NSString *)language url:(NSString *)url completion:(void (^)(BOOL success, NSString *result))completion {
    self.completeBlock = completion;
    [self createSpeech:language url:url];
}

- (void)createSpeech:(NSString *)language url:(NSString *)url {
    // 初始化一个识别器
    self.recognizer = [[SFSpeechRecognizer alloc] initWithLocale:[NSLocale localeWithLocaleIdentifier:language]];
    // 初始化mp3的url
    NSURL *uRL = [NSURL fileURLWithPath:url];
    // 初始化一个识别的请求
    SFSpeechURLRecognitionRequest *request = [[SFSpeechURLRecognitionRequest alloc] initWithURL:uRL];
    
    self.recognitionTask = [self.recognizer recognitionTaskWithRequest:request delegate:self];
}

/// 请求语音识别的权限
- (void)requestAuthorization {
    [SFSpeechRecognizer requestAuthorization:^(SFSpeechRecognizerAuthorizationStatus status) {
        NSLog(@"%ld",(long)status);
        switch (status) {
            case SFSpeechRecognizerAuthorizationStatusNotDetermined:
                NSLog(@"设备不支持");// Device isn't permitted
                break;
            case SFSpeechRecognizerAuthorizationStatusDenied:
                NSLog(@"用户拒绝授权");// User said no
                break;
            case SFSpeechRecognizerAuthorizationStatusRestricted:
                NSLog(@"暂时不清楚"); // Don't know yet
                break;
            case SFSpeechRecognizerAuthorizationStatusAuthorized:
                NSLog(@"授权成功");// Good to go
                break;
            default:
                break;
        }
    }];
}

#pragma mark - speechRecognitionTask 代理
- (void)speechRecognitionTask:(SFSpeechRecognitionTask *)task didFinishRecognition:(SFSpeechRecognitionResult *)recognitionResult {
    NSString *result = recognitionResult.bestTranscription.formattedString;
     NSLog(@"[Speech] 音频转写结果: %@", result);
    
    if (self.completeBlock) {
        self.completeBlock(YES, result);
    }
    
    if ([self.delegate respondsToSelector:@selector(didFinishRecognition:isSuccess:)]) {
        [self.delegate didFinishRecognition:result isSuccess:YES];
    }
}

- (void)speechRecognitionTask:(SFSpeechRecognitionTask *)task didFinishSuccessfully:(BOOL)successfully {
    if (!successfully) {
        NSLog(@"[Speech] 解析失败");
        if (self.completeBlock) {
            self.completeBlock(NO, @"");
        }
        
        if ([self.delegate respondsToSelector:@selector(didFinishRecognition:isSuccess:)]) {
            [self.delegate didFinishRecognition:@"" isSuccess:NO];
        }
        
    } else {
        NSLog(@"[Speech] 全部解析完毕");
    }
}

@end
